package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Slf4j
@Controller
public class SeckillController {
    /**
     * 返回秒杀信息
     *
     * @return
     */
    @Autowired
    SeckillService seckillService;
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> res = seckillService.getCurrentSeckillSkus();
        return R.ok().put("data", res);
    }
    @ResponseBody
    @GetMapping("/sku/seckill/{skuIf}")
    public R getSeckillSkuInfo(@PathVariable("skuIf") Long skuIf) {
        log.debug("skuId:{}", skuIf);
        SeckillSkuRedisTo res = seckillService.getSeckillSkuInfo(skuIf);
        return R.ok().put("data", res);
    }

    @GetMapping("/kill")
    public String getSeckillSkuInfo(@RequestParam("killId")String killId, @RequestParam("key") String key, @RequestParam("num") Integer num, Model model) {
        log.debug("killId:{},key:{},num:{}", killId, key, num);
        String orderSn = seckillService.kill(killId, key, num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }
}
